﻿
//***********************************************************************
// * Module:   TbBstWantedWireHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/7
// * Purpose: Definition of the Class TbBstWantedWireHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstWantedWireHandler 的摘要说明
    /// </summary>
    public class TbBstWantedWireHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstWantedWire mTbBstWantedWire = new TbBstWantedWire();
                mTbBstWantedWire.Id = Guid.NewGuid().ToString(); //主键
                mTbBstWantedWire.Wantedname = context.Request.Form["texWantedname"]; //涉及人员
                mTbBstWantedWire.Name = context.Request.Form["texName"]; //提供人员
                mTbBstWantedWire.Phone = context.Request.Form["texPhone"]; //手机号
                mTbBstWantedWire.Email = context.Request.Form["texEmail"]; //邮箱
                mTbBstWantedWire.Address = context.Request.Form["texAddress"]; //地址
                mTbBstWantedWire.Content = context.Request.Form["texContent"]; //内容
                mTbBstWantedWire.Deltag = 0; //删除标志
                mTbBstWantedWire.Modifydate = DateTime.Now; //添加时间

                mResultModel = TbBstWantedWireBLL.Insert(mTbBstWantedWire, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstWantedWire mTbBstWantedWire = new TbBstWantedWire();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstWantedWireBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstWantedWire mTbBstWantedWire = new TbBstWantedWire();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstWantedWire = TbBstWantedWireBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstWantedWire != null)
                {
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    mTbBstWantedWire.Wantedname = DES.Encode(context.Request.Form["texWantedname"]); //涉及人员
                    mTbBstWantedWire.Name = DES.Encode(context.Request.Form["texName"]); //提供人员
                    mTbBstWantedWire.Phone = DES.Encode(context.Request.Form["texPhone"]); //手机号
                    mTbBstWantedWire.Email = DES.Encode(context.Request.Form["texEmail"]); //邮箱
                    mTbBstWantedWire.Address = DES.Encode(context.Request.Form["texAddress"]); //地址
                    mTbBstWantedWire.Content = DES.Encode(context.Request.Form["texContent"]); //内容
                    mTbBstWantedWire.Deltag = 0; //删除标志
                    mTbBstWantedWire.Modifydate = DateTime.Now; //修改时间
                    mTbBstWantedWire.Modifyuser = mUser.UserName; //修改人

                    mResultModel = TbBstWantedWireBLL.Update(mTbBstWantedWire, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstWantedWire mTbBstWantedWire = new TbBstWantedWire(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstWantedWire = TbBstWantedWireBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstWantedWire != null)
                    {
                        if (mTbBstWantedWire.Modifydate.ToString("yyyy") == "0001")
                        {
                            mTbBstWantedWire.Modifydatestr = "暂无修改";
                        }
                        else
                        {
                            mTbBstWantedWire.Modifydatestr = mTbBstWantedWire.Modifydate.ToString("yyyy-MM-dd HH:mm:ss");
                        }
                        ////数据库部分加密字段解密
                        mTbBstWantedWire.Wantedname = DES.Decode(mTbBstWantedWire.Wantedname);
                        mTbBstWantedWire.Name = DES.Decode(mTbBstWantedWire.Name);
                        mTbBstWantedWire.Phone = DES.Decode(mTbBstWantedWire.Phone);
                        mTbBstWantedWire.Address = DES.Decode(mTbBstWantedWire.Address);
                        mTbBstWantedWire.Email = DES.Decode(mTbBstWantedWire.Email);
                        IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                        json = JsonConvert.SerializeObject(mTbBstWantedWire, dtConverter);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string wantedname = DES.Encode(context.Request.Form["texWantedname"]);////涉嫌人员
            string name = DES.Encode(context.Request.Form["texName"]);////提供人员
            string phone = DES.Encode(context.Request.Form["texPhone"]);
            string timeStart = context.Request.Form["texTimeStart"]; //开始时间
            string timeEnd = context.Request.Form["texTimeEnd"]; //结束时间
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstWantedWire = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList sqllist = new SqlConditionList();
            string sqlWhere = " deltag=0 ";

            if (!string.IsNullOrEmpty(wantedname))////涉嫌人员
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstWantedWireConst.Columns.Wantedname, '%' + wantedname + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(name))////提供人员
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstWantedWireConst.Columns.Name, name), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(phone))////提供人员
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstWantedWireConst.Columns.Phone, phone), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(phone))////审核状态
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstWantedWireConst.Columns.Phone, phone), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(timeStart))
            {
                DateTime dtstart = new DateTime();
                if (DateTime.TryParse(timeStart, out dtstart))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstWantedWireConst.Columns.Createdate, dtstart), SqlLogicOper.AND_LOGIC);
                }
            }
            if (!string.IsNullOrEmpty(timeEnd))
            {
                DateTime dtend = new DateTime();
                if (DateTime.TryParse(timeEnd, out dtend))
                {
                    sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstWantedWireConst.Columns.Createdate, dtend), SqlLogicOper.AND_LOGIC);
                }
            }

            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where " + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }

            dtTbBstWantedWire = Q.GetRecordByPage("ID,WANTEDNAME,NAME,PHONE,EMAIL,ADDRESS,CONTENT,DELTAG,CREATEDATE", sqlWhere, " CREATEDATE", intPageSize, intPageIndex, TbBstWantedWireConst.TableName, TbBstWantedWireConst.FieldKey, "desc").Tables[0];

            //  dtTbBstWantedWire = Q.GetRecordByPage("*", "", "", intPageSize, intPageIndex, TbBstWantedWireConst.TableName, TbBstWantedWireConst.FieldKey, "").Tables[0];
            if (dtTbBstWantedWire != null && dtTbBstWantedWire.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstWantedWire.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstWantedWire.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstWantedWire;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            for (int j = 0; j < dtTbBstWantedWire.Rows.Count; j++)
            {
                for (int m = 1; m < 4; m++)
                {
                    string decode = dtTbBstWantedWire.Rows[j][m].ToString();
                    dtTbBstWantedWire.Rows[j][m] = DES.Decode(decode);
                }
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

    }
}


